package com.sdy.hospitalguard.web.controller;

import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.hospitalguard.biz.dto.RegulationsDTO;
import com.sdy.hospitalguard.biz.service.ManagerService;
import com.sdy.hospitalguard.biz.service.RegulationsService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 注释
 * </p>
 *
 * @author fb
 * @since 2020/5/27
 */
@Slf4j
@RestController
@RequestMapping("/regulations")
@Api(tags = "规则池接口")
public class RegulationsController extends BaseController{

    @Autowired
    private RegulationsService regulationsService;
    @Autowired
    private ManagerService managerService;

    @GetMapping("/select")
    @ApiOperation(value = "规则展示", response = RegulationsDTO.class)
    public Response select(HttpServletRequest request) {
        Integer userId = getUserId(request);
        Integer roleId = managerService.getById(userId).getRoleId();
        if (roleId == 3 || roleId == 4) {
            return Response.error("当前用户不显示此界面!");
        }
        return Response.success(regulationsService.getAllregulations());
    }

    @PostMapping("/update")
    @ApiOperation(value = "规则修改")
    public Response update(@RequestBody RegulationsDTO regulationsDTO, HttpServletRequest request) throws BizException{
        Integer userId = getUserId(request);
        Integer roleId = managerService.getById(userId).getRoleId();
        if (roleId == 3 || roleId == 4) {
            return Response.error("当前用户不显示此界面!");
        }
        return Response.success(regulationsService.updateAllregulations(regulationsDTO));
    }

}
